<?php

include_once "function_xml_basic.php";

function JudgeObjectOrGroup($name)
{	
	$list_o = GetObjectListByNameValue($name);
	if ($list_o->item(0) != NULL)
		return "OBJECT";

	$list_g = GetGroupListByNameValue($name);
	if ($list_g->item(0) != NULL)
		return "GROUP";
	
	return "ERR_NO_NODE";
}

function GetNetObjectAddressValue($name)
{
	$ret_list = GetObjectListByNameValue($name);
	$ret_string_list = array ();
	foreach ($ret_list as $ret)
	{
		$array = array("IpType" => $ret->getAttribute("IpType"),
			       "Ip1" => $ret->getAttribute("Ip1"),
			       "Ip2" => $ret->getAttribute("Ip2"),
			       "NetMask" =>  $ret->getAttribute("NetMask"));
		if ($array["IpType"] == 1) //Net
		{
			$ret_string_list["IpString"] = $array["Ip1"]. "/". $array["NetMask"];
			$ret_string_list["IpType"] = 1;
		}
		if ($array["IpType"] == 2) //Single
		{
			$ret_string_list["IpString"] = $array["Ip1"];
			$ret_string_list["IpType"] = 2;
		
		}
		if ($array["IpType"] == 0) //Continue
		{
			$ret_string_list["IpString"] = $array["Ip1"]. "-". $array["Ip2"];
			$ret_string_list["IpType"] = 0;
		}
	}	
	return $ret_string_list;	
}

function GetNetGroupAddressValue($name)
{
	$ret_list = GetGroupListByNameValue($name);
	foreach ($ret_list as $ret)
	{
		$temp = $ret->getAttribute("ObjectList");
	}
	$ret_list = explode(",", $temp);
	return $ret_list;
}

function GetRealNetValueByName($name)
{
	$i = 0;
	$ret_list = array ();
	$object_type = JudgeObjectOrGroup($name);
	if ($object_type == "OBJECT")
	{
		$ret_list[$i++] = GetNetObjectAddressValue($name);
	}
	if ($object_type = "GROUP")
	{
		$list = GetNetGroupAddressValue($name);
		foreach ($list as $ret)
		{
			$ret_list[$i++] = GetNetObjectAddressValue($ret);
		}
	}
	return $ret_list;
}
/*
$ret_list = GetRealNetValueByName("TestGroup");
foreach ($ret_list as $ret)
{
	print $ret. "\n";
}
*/

function GetServiceObjectAddressValue($name)
{
	$ret_list = GetObjectListByNameValue($name);
	$ret_string_list = array ();
	foreach ($ret_list as $ret)
	{
		$ret_string_list = array("Protocol" => $ret->getAttribute("Protocol"),
			       "ObjectType" => $ret->getAttribute("ObjectType"),
			       "IcmpType" => $ret->getAttribute("IcmpType"),
			       "DstStartPort" =>  $ret->getAttribute("DstStartPort"),
			       "DstEndPort" =>  $ret->getAttribute("DstEndPort"),
			       "SrcStartPort" =>  $ret->getAttribute("SrcStartPort"),
			       "SrcEndPort" =>  $ret->getAttribute("SrcEndPort"));
	}	
	return $ret_string_list;
}
function GetRealServiceValueByName($name)
{
	$i = 0;
	$ret_list = array ();
	$object_type = JudgeObjectOrGroup($name);
	if ($object_type == "OBJECT")
	{
		$ret_list[$i++] = GetServiceObjectAddressValue($name);
	}
	// Current config have not group :(
	if ($object_type = "GROUP")
	{
		$list = GetNetGroupAddressValue($name);
		foreach ($list as $ret)
		{
			$ret_list[$i++] = GetServiceObjectAddressValue($ret);
		}
	}
	return $ret_list;
}
/*
$ret_list = GetRealServiceValueByName("ServiceGroup");
foreach ($ret_list as $ret)
{
	print $ret['Protocol']. "\n";
}
*/
function GetTimeValue($name)
{
	return $name;
}

//ApplySecFilterRule();
?>
